const Router = require('@koa/router');
const router = new Router();


router.post('/getAudio', async (ctx) => {
    const { text } = ctx.request.body;
    console.log(text);
    const token = 'SKKa_RXNaI7mj7TduTHoOLF-z9cpbweQ'
    const APP_ID = 5267964253
    const APP_KEY = '8lkSJifLa175yNTYrOyiPdHqPDDM0d6j'
    // 修改前
    // const entPoint = '/tts/api/v1/tts';

    // 修改后
    // 直接使用完整URL，避免依赖代理配置
    const entPoint = 'https://openspeech.bytedance.com/api/v1/tts';
    const Header = {
        "Authorization": `Bearer;${token}`,
        'Content-Type': 'application/json'
    }
    const payload = {
        "app": {
            "appid": APP_ID,
            "token": APP_KEY,
            "cluster": "volcano_tts",
        },
        "user": {
            "uid": "uid123"
        },
        "audio": {
            "voice_type": "zh_male_junlangnanyou_emo_v2_mars_bigtts",
            "emotion": "tender",
            "encoding": "ogg_opus",
            "speed_ratio": 1.0,
            "volume_ratio": 1.0,
            "pitch_ratio": 1.0,
        },
        "request": {
            "reqid": Date.now(),
            "text": text,
            "operation": "query",
        }
    }
    // setStatus('生成中...')
    const res = await fetch(entPoint, {
        method: 'POST',
        headers: Header,
        body: JSON.stringify(payload),
        credentials: 'include'  // 添加credentials选项
    })
    const data = await res.json()
    console.log(data);
    ctx.body = {
        data: data.data
    }
    ctx.status = 200
})

module.exports = router